﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MaxAlarm.Core.Settings;
using MaxAlarm.Pages;

namespace MaxAlarm.ViewModels
{
    public class MainWindowViewModel : DependencyObject
    {
        #region Properties

        #region Buttons properties

        public static readonly DependencyProperty CanRunProperty =
             DependencyProperty.Register("CanRun", typeof(bool), typeof(MainWindowViewModel), new PropertyMetadata(default(bool)));

        public bool CanRun
        {
            get { return (bool)GetValue(CanRunProperty); }
            set { SetValue(CanRunProperty, value); }
        }

        public static readonly DependencyProperty CanRefreshProperty =
            DependencyProperty.Register("CanRefresh", typeof (bool), typeof (MainWindowViewModel), new PropertyMetadata(default(bool)));

        public bool CanRefresh
        {
            get { return (bool) GetValue(CanRefreshProperty); }
            set { SetValue(CanRefreshProperty, value); }
        }

        public static readonly DependencyProperty CanShowSettingsProperty =
            DependencyProperty.Register("CanShowSettings", typeof (bool), typeof (MainWindowViewModel), new PropertyMetadata(default(bool)));

        public bool CanShowSettings
        {
            get { return (bool) GetValue(CanShowSettingsProperty); }
            set { SetValue(CanShowSettingsProperty, value); }
        }

        #endregion

        public MaxAlarmSettings Settings
        {
            get { return SettingsManager.Instance.Settings; }
        }

        public static readonly DependencyProperty MainFrameProperty =
            DependencyProperty.Register("MainFrame", typeof (Frame), typeof (MainWindowViewModel));

        public Frame MainFrame
        {
            get { return (Frame) GetValue(MainFrameProperty); }
            set { SetValue(MainFrameProperty, value); }
        }

        

        public static readonly DependencyProperty PageManagerProperty =
            DependencyProperty.Register("PageManager", typeof (PageManager), typeof (MainWindowViewModel));

        public PageManager PageManager
        {
            get { return (PageManager) GetValue(PageManagerProperty); }
            set { SetValue(PageManagerProperty, value); }
        }

        public static readonly DependencyProperty EnableSettingsProperty =
            DependencyProperty.Register("EnableSettings", typeof (bool), typeof (MainWindowViewModel), new PropertyMetadata(true));

        public bool EnableSettings
        {
            get { return (bool) GetValue(EnableSettingsProperty); }
            set { SetValue(EnableSettingsProperty, value); }
        }

        #endregion

        #region Commands

        private static ICommand _settingsCommand = new RoutedUICommand("Settings", "SettingsCommand", typeof(MainWindowViewModel));

        public static ICommand SettingsCommand
        {
            get { return _settingsCommand; }
        }

        #endregion

        static MainWindowViewModel()
        {
            CommandManager.RegisterClassCommandBinding(typeof(MainWindow), new CommandBinding(SettingsCommand, SettingsExecuted));
            CommandManager.RegisterClassInputBinding(typeof(MainWindow), new InputBinding(SettingsCommand, new KeyGesture(Key.P, ModifierKeys.Control)));
        }

        private static void SettingsExecuted(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            var window = (MainWindow)sender;
            var viewModel = window.ViewModel;

            viewModel.PageManager.SetPage(viewModel.PageManager.SettingsPage);
        }

        public MainWindowViewModel()
        {
            SettingsManager.Instance.Initialize();
        }
    }
}
